package com.huangmb.anymark.app.api;

import android.support.annotation.NonNull;
import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.cloud.CloudItem;
import com.amap.api.services.cloud.CloudItemDetail;
import com.amap.api.services.cloud.CloudResult;
import com.amap.api.services.cloud.CloudSearch;
import com.amap.api.services.core.AMapException;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.app.AnyMarkApplication;
import com.huangmb.anymark.app.app.AppConstants;
import com.huangmb.anymark.app.model.User;
import com.huangmb.anymark.app.net.HttpRequest;
import com.huangmb.anymark.app.net.RequestParameter;
import com.huangmb.anymark.app.api.mock.UserMarkerApiMock;
import com.huangmb.anymark.app.model.UserMarker;
import com.huangmb.anymark.app.utils.PointConvertor;
import org.jsoup.Connection;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/21 14:27
 */
public class UserMarkerApi extends BaseApi {
    private static final String BASE_PATH = API_SERVER + "/marker";
    private static final String GET_ONE_POI = BASE_PATH + "/%s";
    private static final String GET_POI_LIST = BASE_PATH + "/list";
    private static final String MY_POI_LIST = GET_POI_LIST + "?action=my";
    private static final String NEARBY = BASE_PATH + "/nearby";
    private static final String CREATE_POI = BASE_PATH;
    private static final String DELETE_POI = BASE_PATH + "/%s";
    private static final String MODIFY_POI = BASE_PATH ;
    private static final String CATEGORY = BASE_PATH + "/category";

    private static boolean isMock = false;

    public static void nearby(@NonNull LatLng pos, int range, String keyWord, String sinceID, boolean simplify, @NonNull Listener listener) {
        if (isMock) {
            UserMarkerApiMock.nearby(pos, range, keyWord, sinceID, simplify, listener);
            return;
        }
       /* try {
            CloudSearch search = new CloudSearch(AnyMarkApplication.getInstance());
            search.setOnCloudSearchListener(new CloudSearch.OnCloudSearchListener() {
                @Override
                public void onCloudSearched(CloudResult cloudResult, int i) {
                    ArrayList<CloudItem> items = cloudResult.getClouds();

                    if (items != null) {
                        for (CloudItem item : items) {
                            Logger.d("Cloud", item.getTitle() + "-" + item.getDistance());

                        }
                    }
                }

                @Override
                public void onCloudItemDetailSearched(CloudItemDetail cloudItemDetail, int i) {

                }
            });
            CloudSearch.SearchBound searchBound = new CloudSearch.SearchBound(PointConvertor.convert(pos), range);

            CloudSearch.Query query = new CloudSearch.Query(AppConstants.CLOUD_TABLE_ID, null, searchBound);
            search.searchCloudAsyn(query);
            Logger.d("Cloud", "lat:" + pos.latitude + ";lon:" + pos.longitude + ";range:" + range);
        } catch (AMapException e) {
            e.printStackTrace();
        }*/
        Map<String, String> params = new HashMap<>();
        params.put("latitude", "" + pos.latitude);
        params.put("longitude", "" + pos.longitude);
        params.put("radius", "" + range);
        if (keyWord != null) {
           params.put("keyword", keyWord);
            /*try {
                params.put("keyword", URLEncoder.encode(keyWord,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }*/
        }
        //params.put("since_id", sinceID);
        execute(NEARBY, Connection.Method.GET, params, listener);
    }

    public static void get(@NonNull String id, boolean simplify, @NonNull Listener listener) {
        if (isMock) {
            UserMarkerApiMock.get(id, simplify, listener);
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("simplify", String.valueOf(simplify));
        String url = String.format(GET_ONE_POI, id);
        execute(url, Connection.Method.GET, params, listener);
    }

    public static void create(@NonNull UserMarker item, Listener listener) {
        if (isMock) {
            UserMarkerApiMock.create(item, listener);
            return;
        }
        User user = AnyMarkApplication.getInstance().getUserContext().getUser();
        Map<String, String> params = new HashMap<>();
        params.put("title", item.getTitle());
        params.put("info", item.getInfo());
        params.put("latitude", "" + item.getLatitude());
        params.put("longitude", "" + item.getLongitude());
        params.put("address", item.getAddress());
        params.put("coverImage",item.getCoverImage());
        params.put("category.id", "" + item.getCategory().getId());
        params.put("user.id", "" + user.getId());
        execute(CREATE_POI, Connection.Method.POST, params, listener);
    }

    public static void delete(String id, Listener listener) {
        if (isMock) {
            UserMarkerApiMock.delete(id, listener);
            return;
        }
        String url = String.format(DELETE_POI, id);
        execute(url, Connection.Method.DELETE,null,listener);
    }

    public static void modify(@NonNull UserMarker item, Listener listener) {
        if (isMock) {
            UserMarkerApiMock.modify(item, listener);
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("id",item.getId());
        params.put("title", item.getTitle());
        params.put("info", item.getInfo());
        params.put("latitude", "" + item.getLatitude());
        params.put("longitude", "" + item.getLongitude());
        params.put("address", item.getAddress());
        params.put("coverImage",item.getCoverImage());
        params.put("category.id", "" + item.getCategory().getId());
        execute(MODIFY_POI, Connection.Method.PUT, params, listener);
    }

    public static void myList(Listener listener){
        execute(MY_POI_LIST, Connection.Method.GET,null,listener);
    }

    public static void getAllCategory(Listener listener) {
        execute(CATEGORY, Connection.Method.GET, null, listener);
    }
}
